package com.zaofan.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;

import com.zaofan.bean.Express;
import com.zaofan.bean.ExpressBuilder;
import com.zaofan.bean.OpenBuilder;
import com.zaofan.dao.ExpressDao;
import com.zaofan.exception.ServiceException;
import com.zaofan.utils.C3p0DataSourceUtils;
import com.zaofan.utils.SqlBuilderUtils;
import com.zaofan.utils.WebUtils;

public class ExpressService {

	private ExpressDao expressDao = new ExpressDao();
	private OpenBuilderService openBuilderService = new OpenBuilderService();
	private ExpressBuilderService expressBuilderService = new ExpressBuilderService();

	public void delete(Express express) {
		expressDao.del(express);
	}

	public void add(Express express) throws ServiceException {
		if (WebUtils.isEmpty(express.getName())) {
			throw new ServiceException("请填写正确的送餐员的名称!!");
		}
		if (this.findByName(express.getName()) != null) {
			throw new ServiceException("改送餐员的名称已经存在!!");
		}
		if (WebUtils.isEmpty(express.getPhone())) {
			throw new ServiceException("请填写正确的送餐员的联系电话!!");
		}
		if (WebUtils.isEmpty(express.getAddress())) {
			throw new ServiceException("请填写正确的送餐员的地址!!");
		}
		if (WebUtils.isEmpty(express.getAge())) {
			throw new ServiceException("请填写正确的送餐员的年龄!!");
		}
		express.setPassword("123456");
		express.setPassword(WebUtils.MD5(express.getPassword()));
		express.setAddTime(new Date());
		express.setAddUser("admin");
		expressDao.add(express);
	}

	private Express findByName(String name) {
		Map<String, Object> params = new HashMap<>();
		params.put("name", name);
		return expressDao.findOne(params);
	}

	public void update(Express express) throws ServiceException {
		if (WebUtils.isEmpty(express.getId())) {
			throw new ServiceException("请填写正确的id!!");
		}
		Express target = this.findById(express.getId());
		if (target == null) {
			throw new ServiceException("请填写正确的id!!");
		}
		expressDao.update(express);
	}

	public void updateExpressInfo(Express express) throws ServiceException {
		if (WebUtils.isEmpty(express.getId())) {
			throw new ServiceException("请填写正确的用户id!!");
		}
		Express target = this.findById(express.getId());
		if (target == null) {
			throw new ServiceException("请填写正确的用户id!!");
		}
		if (!WebUtils.isEmpty(express.getName())) {
			target.setName(express.getName());
		}
		if (!WebUtils.isEmpty(express.getPhone())) {
			target.setPhone(express.getPhone());
		}
		if (!WebUtils.isEmpty(express.getAddress())) {
			target.setAddress(express.getAddress());
		}
		if (!WebUtils.isEmpty(express.getSex())) {
			target.setSex(express.getSex());
		}
		if (!WebUtils.isEmpty(express.getAge())) {
			target.setAge(express.getAge());
		}
		target.setUpdateTime(new Date());
		target.setUpdateUser("admin");
		expressDao.update(target);
	}

	public List<Express> findAll(Map<String, Object> params) {
		List<Express> data = expressDao.findAll(params);
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}

	public Long count(Map<String, Object> params) {
		return expressDao.count(params);
	}

	public Express findById(Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		return expressDao.findOne(params);
	}

	public void setStateByCarrierId(int id, int state) {
		expressDao.setStateByCarrierId(id, state);
	}

	public void setExpressState(Express express) throws ServiceException {
		update(express);
	}

	public void updateExpressBuilderInfo(Integer id, String builderIds)
			throws ServiceException {
		try {
			Express express = this.findById(id);
			if (express == null) {
				throw new ServiceException("运营商信息有误");
			}
			Connection connection = C3p0DataSourceUtils.getConnection();
			QueryRunner runner = new QueryRunner();
			String sql = "";
			try {
				connection.setAutoCommit(false);
				String ids = expressBuilderService
						.findByExpressIdBuilderToIds(id);
				sql = "delete from expressbuilder where expressId = " + id;
				System.err.println(sql);
				runner.update(connection, sql);
				ExpressBuilder expressBuilder;
				OpenBuilder openBuilder;
				if (builderIds != null && !"".equals(builderIds))
					for (String builderId_s : builderIds.split(",")) {
						Integer builderId = Integer.valueOf(builderId_s);
						if (ids.contains(builderId_s)) {
							continue;
						}
						openBuilder = openBuilderService.findById(builderId);
						if (openBuilder == null) {
							throw new ServiceException("大楼信息有误!!!");
						}
						openBuilder.setExpressId(id);
						openBuilder.setExpressName(express.getName());
						sql = SqlBuilderUtils.builderUpdateSql(
								OpenBuilder.class, openBuilder);
						System.err.println(sql);
						runner.update(connection, sql);
						sql = "delete from expressbuilder where builderId = "
								+ builderId;
						System.err.println(sql);
						runner.update(connection, sql);
						expressBuilder = new ExpressBuilder();
						expressBuilder.setExpressId(express.getId());
						expressBuilder.setAreaId(openBuilder.getAreaId());
						expressBuilder.setBuilderId(builderId);
						sql = SqlBuilderUtils.builderInsertSql(
								ExpressBuilder.class, expressBuilder);
						runner.update(connection, sql);
						System.err.println(sql);
					}
				connection.commit();
			} catch (ServiceException e) {
				e.printStackTrace();
				throw e;
			} catch (Exception e) {
				connection.rollback();
				e.printStackTrace();
				throw e;
			} finally {
				connection.close();
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException("订单信息有误!!");
		}
	}

}
